home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 007 (1987-02-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 007 (1987-02-15)(Ossowski, Stefan)(DE)(PD).adf / C Compiler / decl.c < prev    next >
C/C++ Source or Header  |  1987-03-04  |  17KB  |  487 lines

  1. #include        <stdio.h>
  2. #include        "c.h"
  3. #include        "expr.h"
  4. #include        "gen.h"
  5. #include        "cglbdec.h"
  6.  
  7. /*
  8.  *    68000 C compiler
  9.  *
  10.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  11.  *    all commercial rights reserved.
  12.  *
  13.  *    This compiler is intended as an instructive tool for personal use. Any
  14.  *    use for profit without the written consent of the author is prohibited.
  15.  *
  16.  *    This compiler may be distributed freely for non-commercial use as long
  17.  *    as this notice stays intact. Please forward any enhancements or questions
  18.  *    to:
  19.  *
  20.  *        Matthew Brandt
  21.  *        Box 920337
  22.  *        Norcross, Ga 30092
  23.  */
  24.  
  25. TYP             *head = 0;
  26. TYP             *tail = 0;
  27. char            *declid = 0;
  28. TABLE           tagtable = {0,0};
  29. TYP             stdconst = { bt_long, 1, 4, {0, 0}, 0, "stdconst"};
  30.  
  31. int     imax(i,j)
  32. int     i,j;
  33. {       return (i > j) ? i : j;
  34. }
  35.  
  36. char    *litlate(s)
  37. char    *s;
  38. {       char    *p;
  39.         p = xalloc(strlen(s) + 1);
  40.         strcpy(p,s);
  41.         return p;
  42. }
  43.  
  44. TYP     *maketype(bt,siz)
  45. enum e_bt bt;
  46. int siz;
  47. {       TYP     *tp;
  48.         tp = xalloc(sizeof(TYP));
  49.         tp->val_flag = 0;
  50.         tp->size = siz;
  51.         tp->type = bt;
  52.         tp->sname = 0;
  53.         tp->lst.head = 0;
  54.         return tp;
  55. }
  56.  
  57. int     decl(table)
  58. TABLE   *table;
  59. {       switch (lastst) {
  60.                 case kw_char:
  61.                         head = tail = maketype(bt_char,1);
  62.                         getsym();
  63.                         break;
  64.                 case kw_short:
  65.                         head = tail = maketype(bt_short,2);
  66.                         getsym();
  67.                         break;
  68.                 case kw_int: case kw_long:
  69.                         head = tail = maketype(bt_long,4);
  70.                         getsym();
  71.                         break;
  72.                 case kw_unsigned:
  73.                         head = tail = maketype(bt_unsigned,4);
  74.                         getsym();
  75.                         if( lastst == kw_int )
  76.                                 getsym();
  77.                         break;
  78.                 case id:                /* no type declarator */
  79.                         head = tail = maketype(bt_long,4);
  80.                         break;
  81.                 case kw_float:
  82.                         head = tail = maketype(bt_float,4);
  83.                         getsym();
  84.                         break;
  85.                 case kw_double:
  86.                         head = tail = maketype(bt_double,8);
  87.                         getsym();
  88.                         break;
  89.                 case kw_enum:
  90.                         getsym();
  91.                         declenum(table);
  92.                         break;
  93.                 case kw_struct:
  94.                         getsym();
  95.                         declstruct(bt_struct);
  96.                         break;
  97.                 case kw_union:
  98.                         getsym();
  99.                         declstruct(bt_union);
  100.                         break;
  101.                 }
  102. }
  103.  
  104. decl1()
  105. {       TYP     *temp1, *temp2, *temp3, *temp4;
  106.         switch (lastst) {
  107.                 case id:
  108.                         declid = litlate(lastid);
  109.                         getsym();
  110.                         decl2();
  111.                         break;
  112.                 case star:
  113.                         temp1 = maketype(bt_pointer,4);
  114.                         temp1->btp = head;
  115.                         head = temp1;
  116.                         if(tail == NULL)
  117.                                 tail = head;
  118.                         getsym();
  119.                         decl1();
  120.                         break;
  121.                 case openpa:
  122.                         getsym();
  123.                         temp1 = head;
  124.                         temp2 = tail;
  125.                         head = tail = NULL;
  126.                         decl1();
  127.                         needpunc(closepa);
  128.                         temp3 = head;
  129.                         temp4 = tail;
  130.                         head = temp1;
  131.                         tail = temp2;
  132.                         decl2();
  133.                         temp4->btp = head;
  134.                         if(temp4->type == bt_pointer &&
  135.                                 temp4->val_flag != 0 && head != NULL)
  136.                                 temp4->size *= head->size;
  137.                         head = temp3;
  138.                         break;
  139.                 default:
  140.                         decl2();
  141.                         break;
  142.                 }
  143. }
  144.  
  145. decl2()
  146. {       TYP     *temp1;
  147.         switch (lastst) {
  148.                 case openbr:
  149.                         getsym();
  150.                         temp1 = maketype(bt_pointer,0);
  151.                         temp1->val_flag = 1;
  152.                         temp1->btp = head;
  153.                         if(lastst == closebr) {
  154.                                 temp1->size = 0;
  155.                                 getsym();
  156.                                 }
  157.                         else if(head != NULL) {
  158.                                 temp1->size = intexpr() * head->size;
  159.                                 needpunc(closebr);
  160.                                 }
  161.                         else {
  162.                                 temp1->size = intexpr();
  163.                                 needpunc(closebr);
  164.                                 }
  165.                         head = temp1;
  166.                         if( tail == NULL)
  167.                                 tail = head;
  168.                         decl2();
  169.                         break;
  170.                 case openpa:
  171.                         getsym();
  172.                         temp1 = maketype(bt_func,0);
  173.                         temp1->val_flag = 1;
  174.                         temp1->btp = head;
  175.                         head = temp1;
  176.                         if( lastst == closepa) {
  177.                                 getsym();
  178.                                 if(lastst == begin)
  179.                                         temp1->type = bt_ifunc;
  180.                                 }
  181.                         else
  182.                                 temp1->type = bt_ifunc;
  183.                         break;
  184.                 }
  185. }
  186.  
  187. int     alignment(tp)
  188. TYP     *tp;
  189. {       switch(tp->type) {
  190.                 case bt_char:           return AL_CHAR;
  191.                 case bt_short:          return AL_SHORT;
  192.                 case bt_long:           return AL_LONG;
  193.                 case bt_enum:           return AL_SHORT;
  194.                 case bt_pointer:
  195.                         if(tp->val_flag)
  196.                                 return alignment(tp->btp);
  197.                         else
  198.                                 return AL_POINTER;
  199.                 case bt_float:          return AL_FLOAT;
  200.                 case bt_double:         return AL_DOUBLE;
  201.                 case bt_struct:
  202.                 case bt_union:          return AL_STRUCT;
  203.                 default:                return AL_CHAR;
  204.                 }
  205. }
  206.  
  207. int     declare(table,al,ilc,ztype)
  208. /*
  209.  *      process declarations of the form:
  210.  *
  211.  *              <type>  <decl>, <decl>...;
  212.  *
  213.  *      leaves the declarations in the symbol table pointed to by
  214.  *      table and returns the number of bytes declared. al is the
  215.  *      allocation type to assign, ilc is the initial location
  216.  *      counter. if al is sc_member then no initialization will
  217.  *      be processed. ztype should be bt_struct for normal and in
  218.  *      structure declarations and sc_union for in union declarations.
  219.  */
  220. TABLE           *table;
  221. enum e_sc al;
  222. int             ilc;
  223. enum e_bt ztype;
  224. {       SYM     *sp, *sp1;
  225.         TYP     *dhead;
  226.         int     nbytes;
  227.         nbytes = 0;
  228.         decl(table);
  229.         dhead = head;
  230.         for(;;) {
  231.                 declid = 0;
  232.                 decl1();
  233.                 if( declid != 0) {      /* otherwise just struct tag... */
  234.                         sp = xalloc(sizeof(SYM));
  235.                         sp->name = declid;
  236.                         sp->storage_class = al;
  237.                         while( (ilc + nbytes) % alignment(head)) {
  238.                                 if( al != sc_member &&
  239.                                         al != sc_external &&
  240.                                         al != sc_auto) {
  241.                                         dseg();
  242.                                         genbyte(0);
  243.                                         }
  244.                                 ++nbytes;
  245.                                 }
  246.                         if( al == sc_static)
  247.                                 sp->value.i = nextlabel++;
  248.                         else if( ztype == bt_union)
  249.                                 sp->value.i = ilc;
  250.                         else if( al != sc_auto )
  251.                                 sp->value.i = ilc + nbytes;
  252.                         else
  253.                                 sp->value.i = -(ilc + nbytes + head->size);
  254.                         sp->tp = head;
  255.                         if( sp->tp->type == bt_func && 
  256.                                 sp->storage_class == sc_global )
  257.                                 sp->storage_class = sc_external;
  258.                         if(ztype == bt_union)
  259.                                 nbytes = imax(nbytes,sp->tp->size);
  260.                         else if(al != sc_external)
  261.                                 nbytes += sp->tp->size;
  262.                         if( sp->tp->type == bt_ifunc &&
  263.                                 (sp1 = search(sp->name,table->head)) != 0 &&
  264.                                 sp1->tp->type == bt_func )
  265.                                 {
  266.                                 sp1->tp = sp->tp;
  267.                                 sp1->storage_class = sp->storage_class;
  268.                                 sp1->value.i = sp->value.i;
  269.                                 sp = sp1;
  270.                                 }
  271.                         else
  272.                                 insert(sp,table);
  273.                         if( sp->tp->type == bt_ifunc) { /* function body follows */
  274.                                 funcbody(sp);
  275.                                 return nbytes;
  276.                                 }
  277.                         if( (al == sc_global || al == sc_static) &&
  278.                                 sp->tp->type != bt_func)
  279.                                 doinit(sp);
  280.                         }
  281.                 if(lastst == semicolon)
  282.                         break;
  283.                 needpunc(comma);
  284.                 if(declbegin(lastst) == 0)
  285.                         break;
  286.                 head = dhead;
  287.                 }
  288.         getsym();
  289.         return nbytes;
  290. }
  291.  
  292. int declbegin(st)
  293. enum e_sym st;
  294. {
  295.     return (st == star || st == id || st == openpa || st == openbr);
  296. }
  297.  
  298. declenum(table)
  299. TABLE   *table;
  300. {       SYM     *sp;
  301.         TYP     *tp;
  302.         int     evalue;
  303.         if( lastst == id) {
  304.                 if((sp = search(lastid,tagtable.head)) == 0) {
  305.                         sp = xalloc(sizeof(SYM));
  306.                         sp->tp = xalloc(sizeof(TYP));
  307.                         sp->tp->type = bt_enum;
  308.                         sp->tp->size = 2;
  309.                         sp->tp->lst.head = sp->tp->btp = 0;
  310.                         sp->storage_class = sc_type;
  311.                         sp->name = litlate(lastid);
  312.                         sp->tp->sname = sp->name;
  313.                         getsym();
  314.                         if( lastst != begin)
  315.                                 error(ERR_INCOMPLETE);
  316.                         else    {
  317.                                 insert(sp,&tagtable);
  318.                                 getsym();
  319.                                 enumbody(table);
  320.                                 }
  321.                         }
  322.                 else
  323.                         getsym();
  324.                 head = sp->tp;
  325.                 }
  326.         else    {
  327.                 tp = xalloc(sizeof(tp));
  328.                 tp->type = bt_short;
  329.                 if( lastst != begin)
  330.                         error(ERR_INCOMPLETE);
  331.                 else    {
  332.                         getsym();
  333.                         enumbody(table);
  334.                         }
  335.                 head = tp;
  336.                 }
  337. }
  338.  
  339. enumbody(table)
  340. TABLE   *table;
  341. {       int     evalue;
  342.         SYM     *sp;
  343.         evalue = 0;
  344.         while(lastst == id) {
  345.                 sp = xalloc(sizeof(SYM));
  346.                 sp->value.i = evalue++;
  347.                 sp->name = litlate(lastid);
  348.                 sp->storage_class = sc_const;
  349.                 sp->tp = &stdconst;
  350.                 insert(sp,table);
  351.                 getsym();
  352.                 if( lastst == comma)
  353.                         getsym();
  354.                 else if(lastst != end)
  355.                         break;
  356.                 }
  357.         needpunc(end);
  358. }
  359.  
  360. declstruct(ztype)
  361. /*
  362.  *      declare a structure or union type. ztype should be either
  363.  *      bt_struct or bt_union.
  364.  */
  365. enum e_bt ztype;
  366. {       SYM     *sp;
  367.         TYP     *tp;
  368.         int     slc;
  369.         if(lastst == id) {
  370.                 if((sp = search(lastid,tagtable.head)) == 0) {
  371.                         sp = xalloc(sizeof(SYM));
  372.                         sp->name = litlate(lastid);
  373.                         sp->tp = xalloc(sizeof(TYP));
  374.                         sp->tp->type = ztype;
  375.                         sp->tp->lst.head = 0;
  376.                         sp->storage_class = sc_type;
  377.                         sp->tp->sname = sp->name;
  378.                         getsym();
  379.                         if(lastst != begin)
  380.                                 error(ERR_INCOMPLETE);
  381.                         else    {
  382.                                 insert(sp,&tagtable);
  383.                                 getsym();
  384.                                 structbody(sp->tp,ztype);
  385.                                 }
  386.                         }
  387.                 else
  388.                         getsym();
  389.                 head = sp->tp;
  390.                 }
  391.         else    {
  392.                 tp = xalloc(sizeof(TYP));
  393.                 tp->type = ztype;
  394.                 tp->sname = 0;
  395.                 tp->lst.head = 0;
  396.                 if( lastst != begin)
  397.                         error(ERR_INCOMPLETE);
  398.                 else    {
  399.                         getsym();
  400.                         structbody(tp,ztype);
  401.                         }
  402.                 head = tp;
  403.                 }
  404. }
  405.  
  406. structbody(tp,ztype)
  407. TYP             *tp;
  408. enum e_bt ztype;
  409. {       int     slc;
  410.         slc = 0;
  411.         tp->val_flag = 1;
  412.         while( lastst != end) {
  413.                 if(ztype == bt_struct)
  414.                         slc += declare(&(tp->lst),sc_member,slc,ztype);
  415.                 else
  416.                         slc = imax(slc,declare(&tp->lst,sc_member,0,ztype));
  417.                 }
  418.         tp->size = slc;
  419.         getsym();
  420. }
  421.  
  422. compile()
  423. /*
  424.  *      main compiler routine. this routine parses all of the
  425.  *      declarations using declare which will call funcbody as
  426.  *      functions are encountered.
  427.  */
  428. {       while(lastst != eof) {
  429.                 dodecl(sc_global);
  430.                 if( lastst != eof)
  431.                         getsym();
  432.                 }
  433.         dumplits();
  434. }
  435.  
  436. dodecl(defclass)
  437. enum e_sc defclass;
  438. {       int     size;
  439.         for(;;) {
  440.             switch(lastst) {
  441.                 case kw_register:
  442.                         getsym();
  443.                         if( defclass != sc_auto && defclass != sc_member )
  444.                                 error(ERR_ILLCLASS);
  445.                         goto do_decl;
  446.                 case id:
  447.                         if(defclass == sc_auto)
  448.                                 return;
  449. /*                      else fall through to declare    */
  450.                 case kw_char: case kw_int: case kw_short: case kw_unsigned:
  451.                 case kw_long: case kw_struct: case kw_union:
  452.                 case kw_enum: case kw_void:
  453.                 case kw_float: case kw_double:
  454. do_decl:            if( defclass == sc_global)
  455.                         lc_static +=
  456.                             declare(&gsyms,sc_global,lc_static,bt_struct);
  457.                     else if( defclass == sc_auto)
  458.                         lc_auto +=
  459.                             declare(&lsyms,sc_auto,lc_auto,bt_struct);
  460.                     else
  461.                         declare(&lsyms,sc_auto,0,bt_struct);
  462.                     break;
  463.                 case kw_static:
  464.                         getsym();
  465.                         if( defclass == sc_member)
  466.                             error(ERR_ILLCLASS);
  467.                         if( defclass == sc_auto )
  468.                             lc_static += 
  469.                                 declare(&lsyms,sc_static,lc_static,bt_struct);
  470.                         else
  471.                             lc_static +=
  472.                                 declare(&gsyms,sc_static,lc_static,bt_struct);
  473.                         break;
  474.                 case kw_extern:
  475.                         getsym();
  476.                         if( defclass == sc_member)
  477.                             error(ERR_ILLCLASS);
  478.                         ++global_flag;
  479.                         declare(&gsyms,sc_external,0,bt_struct);
  480.                         --global_flag;
  481.                         break;
  482.                 default:
  483.                         return;
  484.                 }
  485.             }
  486. }
  487.